package com.film.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.ReviewPriaseDao;
import com.film.util.DBUtil;


public class ReviewPriaseDaoImpl implements ReviewPriaseDao {
	private JdbcTemplate temp = new JdbcTemplate(DBUtil.getDataScorse());
	@Override
	public int insertReviewPriase(int uId, int revId) {
		String sql = "insert into reviewPraise value(default,?,?)";
		return temp.update(sql,uId,revId);
	}

	@Override
	public int deleteReviewPriase(int uId, int revId) {
		String sql = "delete from reviewPraise where u_id = ? and rev_id = ?";
		return temp.update(sql,uId,revId);
	}

	@Override
	public List<Integer> selectUserReviewPriase(int uId) {
		Connection conn = null;
		PreparedStatement pre = null;
		ResultSet res = null;
		List<Integer> revIds = new ArrayList();
		try {
			conn = DBUtil.getConnecion();
			String sql = "select DISTINCT rev_id from reviewpraise where u_id = ?"; 
			pre = conn.prepareStatement(sql);
			
			pre.setInt(1,uId);
			
			res = pre.executeQuery();
			while(res.next()) {
				revIds.add(res.getInt("rev_id"));
			}
			
		}catch (Exception e) {
			// TODO: handle exception
		}finally {
			DBUtil.closeAll(res, pre, conn);
		}
		return revIds;
	}

}
